package com.ourteam.product.ui;

import java.io.InputStream;
import java.io.Writer;
import java.util.Collection;

import org.apache.commons.io.IOUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;

import net.ui.model.action.ActionSetBean;
import net.ui.model.constlist.ListDataContainer;
import net.ui.model.form.FormSetConfig;
import net.ui.model.tab.TabbedConfig;
import net.ui.model.table.TableConfig;
import net.ui.model.tree.TreeBean;

public class UIConfigUtils {

	private static final VelocityEngine VELOCITY_ENGINE = new VelocityEngine();

	public static void saveUIActionConfig(ActionSetBean actionSetBean, Writer fileWriter) throws Exception {

		Context context = new VelocityContext();

		InputStream template = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream("com/ourteam/product/ui/ui_action_config.vm");

		context.put("actionSet", actionSetBean);
		context.put("escapeUtils", StringEscapeUtils.class);

		VELOCITY_ENGINE.evaluate(context, fileWriter, "ui", IOUtils.toString(template, "UTF-8"));

	}

	public static void saveUISelectListConfig(Collection<ListDataContainer> listDataContainers, Writer fileWriter)
			throws Exception {

		Context context = new VelocityContext();

		context.put("listDataContainers", listDataContainers);
		
		context.put("escapeUtils", new StringEscapeUtils());

		InputStream template = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream("com/ourteam/product/ui/ui_list_config.vm");

		VELOCITY_ENGINE.evaluate(context, fileWriter, "ui", IOUtils.toString(template, "UTF-8"));
	}

	public static void saveUIFromConfig(FormSetConfig formSetConfig, Writer fileWriter) throws Exception {

		Context context = new VelocityContext();

		context.put("formSetConfig", formSetConfig);
		
		context.put("escapeUtils", new StringEscapeUtils());

		InputStream template = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream("com/ourteam/product/ui/form-config.vm");

		VELOCITY_ENGINE.evaluate(context, fileWriter, "ui", IOUtils.toString(template, "UTF-8"));
	}

	public static void saveUITableConfig(TableConfig tableConfig, Writer fileWriter) throws Exception {

		Context context = new VelocityContext();

		context.put("tableSetConfig", tableConfig);
		
		context.put("escapeUtils", new StringEscapeUtils());

		InputStream template = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream("com/ourteam/product/ui/table-config.vm");

		VELOCITY_ENGINE.evaluate(context, fileWriter, "ui", IOUtils.toString(template, "UTF-8"));
	}

	public static void saveUITabConfig(TabbedConfig tabbedConfig, Writer fileWriter) throws Exception {

		Context context = new VelocityContext();

		context.put("tabbedSetConfig", tabbedConfig);

		InputStream template = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream("com/ourteam/product/ui/tabbed-config.vm");

		VELOCITY_ENGINE.evaluate(context, fileWriter, "ui", IOUtils.toString(template, "UTF-8"));
	}

	public static void saveUITreeConfig(TreeBean treeBean, Writer fileWriter) throws Exception {

		Context context = new VelocityContext();

		context.put("treeConfig", treeBean);
		
		context.put("escapeUtils", new StringEscapeUtils());

		InputStream template = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream("com/ourteam/product/ui/tree-config.vm");

		VELOCITY_ENGINE.evaluate(context, fileWriter, "ui", IOUtils.toString(template, "UTF-8"));
	}

	
}
